package baseFx.common.lock;

import baseFx.common.AutoCloseable;
import baseFx.common.utils.ExceptionUtils;

public interface Lockspace {
    /**
     * 队列锁 - 单个排队进入
     */
    AutoCloseable queueLock(String key, Integer abortTimeout) throws AbortTimeoutException;

    /**
     * 异常锁 - 进入失败直接异常
     */
    AutoCloseable exLock(String key) throws LockspaceException;

    /**
     * 并发锁 - 多个排队进入
     */
    AutoCloseable concurrentLock(String key, int max, Integer abortTimeout) throws AbortTimeoutException;

    default AutoCloseable concurrentLock(String key, int max) {
        try {
            return concurrentLock(key, max, null);
        } catch (AbortTimeoutException e) {
            throw ExceptionUtils.toRuntime(e);
        }
    }

    default AutoCloseable queueLock(String key) {
        try {
            return queueLock(key, null);
        } catch (AbortTimeoutException e) {
            throw ExceptionUtils.toRuntime(e);
        }
    }
}
